﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Easy_66_AddOne
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] digits = new int[] { 9 ,9,9,9};
            int[] digit = PlusOne(digits);
            foreach (var item in digit)
            {
                Console.WriteLine(item);

            }
            Console.ReadKey();
        }
        public static int[] PlusOne(int[] digits)
        {
            bool isRight = true;
            foreach (var item in digits)
            {
                if (item.ToString() != "9")
                {
                    isRight = false;
                    break;
                }
               
            }
            if (isRight)
            {
                int[] temp = new int[digits.Length + 1];
                temp[0] = 1;
                for (int i = 1; i < digits.Length; i++)
                {
                    temp[i] = 0;
                }
                return temp;
            }

            return PlusOneOther(digits, digits.Length - 1);
        }

        private static int[] PlusOneOther(int[] digits,int index)
        {
            int last = digits[index];
            if (last + 1 >= 10)
            {
                digits[index] = 0;
                return PlusOneOther(digits, index-1);
            }
            else
                digits[index] += 1;

            return digits;
        }
    }
}
